namespace ScrumSprintMonitor.Samples.FaroTestWatcher
{
	#region #using Directives

	using Extensibility;

	using Implementation;

	using Infrastructure;

	using Microsoft.Practices.Composite.Modularity;
	using Microsoft.Practices.Composite.Regions;
	using Microsoft.Practices.ServiceLocation;

	using Ninject.Core;
	using Ninject.Core.Behavior;

	using Properties;

	using ViewModels;

	#endregion

	[Module(ModuleName = "FAROTestWatcher")]
	public class FaroTestWatcherModule : Microsoft.Practices.Composite.Modularity.IModule
	{
		private readonly IKernel kernel;
		private readonly ILogger<FaroTestWatcherModule> logger;
		private readonly IRegionManager regionManager;
		private readonly IResourceManager resourceManager;
		private readonly IServiceLocator serviceLocator;

		public FaroTestWatcherModule(IServiceLocator serviceLocator, IKernel container, IRegionManager regionManager, IResourceManager resourceManager,
		                             ILogger<FaroTestWatcherModule> logger)
		{
			this.serviceLocator = serviceLocator;
			this.kernel = container;
			this.regionManager = regionManager;
			this.resourceManager = resourceManager;
			this.logger = logger;

			this.logger.Info("Module starting...");
		}

		#region Implementation of IModule

		public void Initialize()
		{
			RegisterViewsAndServices();

			this.regionManager.RegisterViewWithRegion(RegionNames.SprintStatisticsRegion, () => this.serviceLocator.GetInstance<ITestWatcherViewModel>());
		}

		#endregion

		private void RegisterViewsAndServices()
		{
			this.resourceManager.MergeDictionary(
				"pack://application:,,,/ScrumSprintMonitor.Samples.FaroTestWatcher;component/DataTemplates/TestStatusDataTemplate.xaml");

			var testReportFileStream = new FileDataStreamProvider(Settings.Default.SystemTestReportFilePath,
			                                                      this.serviceLocator.GetInstance<ILogger<FileDataStreamProvider>>());
			this.kernel.Load(
				new InlineModule(
					m => m.Bind<ITestWatcherViewModel>().To<TestWatcherViewModel>().Using<SingletonBehavior>().WithArgument("streamProvider", testReportFileStream)));
		}
	}
}